@extends('layouts.buy')

@section('title', '支付订单')

@section('content')
    <div class="area">
        <h3>订单信息</h3>
        <div class="pay-order-info">
            <div class="item-info">
                <div class="order-field">订单编号</div>
                <div class="order-info">{{$order['order_no']}}</div>
            </div>
            <div class="item-info">
                <div class="order-field">订单金额</div>
                <div class="order-info">{{$order['total_fee']}}</div>
            </div>
            <div class="item-info">
                <div class="order-field">下单时间</div>
                <div class="order-info">{{@date('Y-m-d H:i:s', $order['created_at'])}}</div>
            </div>
            <div class="item-info">
                <div class="order-field">交易流水</div>
                <div class="order-info">{{$order['trade_no']}}</div>
            </div>
        </div>

        <h3>付款方式</h3>
        <div class="pay-type-choose">
            <form method="post">
                {{csrf_field()}}
                <input type="hidden" name="order_id" value="{{$order_id}}">
                <div class="pay-type-div">
                    <div class="type-radio-box">
                        <label>
                            <input type="radio" class="payTypeRadio" name="pay_type" value="balance">
                            <span>余额支付</span>
                        </label>
                    </div>
                    <div class="type-info">
                        <p>当前账户余额:{{$wallet['balance']}}元</p>
                        <p>支付密码:<input title="" type="password" name="password" class="form-control w200" id="password"></p>
                        <p>请输入登录密码进行支付</p>
                    </div>
                </div>
                <div class="pay-type-div">
                    <div class="type-radio-box" style="padding-top: 9px;">
                        <label>
                            <input type="radio" class="payTypeRadio" name="pay_type" value="alipay" checked>
                            <span>支付宝支付</span>
                        </label>
                    </div>
                    <div class="type-info">
                        <dl class="pay-channel">
                            <dd>
                                <span class="pay-icon pay-icon-alipay"></span>
                                <span class="pay-name">支付宝支付</span>
                            </dd>
                        </dl>
                    </div>
                </div>

                <div class="pay-type-div">
                    <div class="type-radio-box">&nbsp;</div>
                    <div class="type-info"><div class="confirm-pay-button" id="confirm-pay-button">确认支付</div></div>
                </div>
            </form>
        </div>
    </div>
    <form method="post" id="FrmQuery" action="{{url('/buy/query')}}">
        <input type="hidden" name="order_id" value="{{$order_id}}">
        <input type="hidden" name="pay_type" value="1" id="J_pay_type">
    </form>
    <script type="text/javascript">
        $(function () {
            $("#confirm-pay-button").on('click', function () {
                var pay_type = $(".payTypeRadio:checked").val();
                if (pay_type === 'balance'){//余额支付
                    var password = $.trim($("#password").val());
                    if (!DSXValidate.IsPassword(password)){
                        DSXUI.error('请输入登录密码');
                        return false;
                    }

                    var total_fee = parseFloat('{{$order['total_fee']}}');
                    var balance = parseFloat('{{$wallet['balance']}}');
                    if (balance < total_fee){
                        DSXUI.error('账户余额不足，请选择其他方式付款');
                        return false;
                    }

                    $.ajax({
                        type:'POST',
                        url:'{{url('/buy/balance_pay')}}',
                        data:{order_id:'{{$order_id}}', password:password},
                        beforeSend:DSXUI.showSpinner,
                        success:function (response) {
                            setTimeout(function () {
                                DSXUI.hideSpinner();
                                if (response.errcode) {
                                    DSXUI.error(response.errmsg);
                                } else {
                                    $("#FrmQuery").submit();
                                }
                            }, 500);
                        }
                    });
                }

                if (pay_type === 'alipay'){
                    var alipay_query = function (callback) {
                        $.ajax({
                            url:"{U:('c=pay&a=wxpay_query&order_id='.$order[order_id])}",
                            dayaType:'json',
                            success:function (response) {
                                if (response.errcode === 0){
                                    if(callback) callback();
                                }
                            }
                        });
                    };
                    window.open("{{url('/buy/alipay?order_id='.$order_id)}}");
                    DSXUI.dialog({
                        title:"支付宝付款",
                        style:{
                            width:'400px'
                        },
                        content:'<div class="tips" style="padding: 20px 0; text-align: center; font-size: 16px;">请在支付宝页面完成付款</div>',
                        afterClose:function (dlg) {
                            alipay_query(function () {
                                window.location.href = "{U:('c=pay&a=alipay_query&order_id='.$order[order_id])}";
                            });
                        }
                    });
                }

                if (pay_type === 'wxpay'){
                    if (pay_channel == 'wxpay'){
                        var qrCode = "{U:('c=pay&a=wxpay_qrcode&order_id='.$order[order_id])}";
                        var queryOrder = function (callback) {
                            $.ajax({
                                url:"{U:('c=pay&a=wxpay_query&order_id='.$order[order_id])}",
                                dayaType:'json',
                                success:function (response) {
                                    if (response.errcode == 0){
                                        if(callback) callback();
                                    }
                                }
                            });
                        }
                        DSXUI.dialog({
                            title:'微信支付',
                            hideTitle:true,
                            yesBtn:'完成付款',
                            width:'400px',
                            content:'<div class="pay-qrcode">' +
                            '<img src="'+qrCode+'" class="qrcode">'+
                            '<div class="tips">用微信扫一扫进行付款</div> '+
                            '</div>',
                            onConfirm:function (dlg) {
                                queryOrder(function () {
                                    dlg.close();
                                    $("#J_Frmpay").submit();
                                });
                            },
                            onCancel:function (dlg) {
                                queryOrder();
                            }
                        });
                    }


                }
            });
        });
    </script>
@stop
